package com.wyh.controlller;

import com.wyh.ann.SysLog;
import com.wyh.dao.InsuranceDao;
import com.wyh.dao.ThreeLevDao;
import com.wyh.entity.Insurance;
import com.wyh.entity.QueryVO;
import com.wyh.entity.ThreeLev;
import com.wyh.service.InsuranceService;
import com.wyh.struct.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author wangyaohan
 * @Date 2021/9/24 11:12
 * @ClassName InsuranceController
 */

@RestController
@RequestMapping("ins")
@CrossOrigin
public class InsuranceController {

    @Autowired
    InsuranceService insuranceService;

    @Autowired
    InsuranceDao insuranceDao;

    @Autowired
    ThreeLevDao threeLevDao;

    /**
     * 列表、查询
     * //1、@Cacheable 的作用  主要针对方法配置，能够根据方法的请求参数对其结果进行缓存
     * //2、@CacheEvict(value = "findInsurance",allEntries = true) 主要针对方法配置，能够根据一定的条件对缓存进行清空
     * //3.1@CachePut("findInsurance")主要针对方法配置，能够根据方法的请求参数对其结果进行缓存，和
     * //3.2@Cacheable 不同的是，它每次都会触发真实方法的调用
     *
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @SysLog("列表")
    @RequestMapping("findInsurance")
    @Cacheable(value = "findInsurance", key = "#vo")
    public Object findInsurance(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                QueryVO vo) {
        return insuranceService.find(pageNum, pageSize, vo);
    }

    /**
     * 新增保单
     *
     * @param insurance
     * @return
     */
    @RequestMapping("save")
    public Object save(@RequestBody Insurance insurance) {
        try {
            insuranceDao.saveAndFlush(insurance);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除保单
     *
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Object delete(@RequestParam("id") Integer id) {
        try {
            insuranceDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("findById")
    public Object findById(@RequestParam("id") Integer id) {
        return insuranceDao.findById(id);
    }

    /**
     * 编辑保单
     *
     * @param insurance
     * @return
     */
    @RequestMapping("update")
    public Object update(@RequestBody Insurance insurance) {
        try {
            insuranceDao.saveAndFlush(insurance);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @PostMapping("saveTree")
    public Object saveTree(@RequestBody ThreeLev threeLev) {
        System.out.println(threeLev);

        return threeLev;
    }

}

